package com.cn.wanxi.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public class UploadTool {
    public static void load(HttpServletRequest req) throws IOException {
        String filename = null;
        String url = "";
//        保存图片，并得到地址
//        工厂，来接受前端传递过来的数据
        DiskFileItemFactory factory = new DiskFileItemFactory();
//        解析文件
        ServletFileUpload upload = new ServletFileUpload(factory);
        factory.setSizeThreshold(1024 * 1024);
        List<FileItem> items = null;
        try {
            items = upload.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
//        Iterator迭代器   单线程
//        List<FileItem> 比较的时候  多线程
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
//            将前端的数据强制转换为文件
            FileItem item = (FileItem) iter.next();
//            判断文件是否为普通表单数据
            if (!item.isFormField()) {
                // 根据时间戳创建头像文件
//                filename = System.currentTimeMillis() + ".jpg";
//                应该保存名字不一样
                filename = System.currentTimeMillis() + "--------" + item.getName();
                String path = req.getServletContext().getRealPath("/");
                String outPath = path;
                path = path.split("out")[0];
                path = path + "/web";
                String directory = "/loadImg/";
                url = directory + filename;
                req.setAttribute("url", url);
                System.out.println(req.getServletContext().getRealPath("/"));
//                File f = new File(getServletContext().getRealPath("/"));
                File f = new File(path, directory);
                if (!f.exists()) {
                    f.mkdirs();
                }
//                String imgsrc = f + "/" + filename;
                // 复制文件
                InputStream is = item.getInputStream();
                File file = new File(f, filename);


                FileOutputStream fos = new FileOutputStream(file);

//                将图片赋值到out文件里面
                File outdirectory = new File(outPath, directory);//这里是out里面的文件夹
                File outFile = new File(outdirectory, filename);//这里out里面文件路径
                FileOutputStream outFileWirte = new FileOutputStream(outFile);
//                -------------------------------------------
                byte b[] = new byte[1024 * 1024];
                int length = 0;
                while (-1 != (length = is.read(b))) {
                    fos.write(b, 0, length);
                    outFileWirte.write(b, 0, length);
                }
                fos.flush();
                fos.close();


            } else {
                System.out.println(item.getFieldName());//key值
                String value = item.getString();//value值
                value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                System.out.println(value);
            }
        }

    }
}
